layui.use(['form', 'table', 'laydate', 'layarea', 'tableSelect'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layarea = layui.layarea;

    let id_arr=$.getQueryVariable('id_arr')

    /*城市级联*/
    layarea.render({
        elem: '#area-picker',
        change: function (res) {
            //选择结果
            submitInfo();
        }
    });
    /*玩家列表*/
    table.render({
        elem: '#currentTableId',
        url: '/admin/Member/index?action=ajax&id_arr='+id_arr,
        method: 'post',
        where: {mark: "{$mark}", address: "{$address}", level: "{$level}", age: "{$age}",'team':"{$team}"},
        parseData: function (res) { //res 即为原始返回的数据
            $('#countNum').html(res.count);

        },
        toolbar: '#toolbarDemo',
        defaultToolbar: [ 'exports'],
        cols: [[
            {field: 'headpic', title: '头像', sort: true,templet:'#thumbTpl'},
            {field: 'name', title: '昵称', sort: true,},
            // {field: 'openid', title: 'openid'},
            // {field: 'openid', title: '设备ID'},
            {field: 'create_time', title: '创建时间', sort: true,},
            {field: 'phone', title: '手机号', sort: true, minWidth: 120},
            {field: 'address_total', title: '地址信息',  },
            {field: 'status_title', title: '状态', },
            {field: 'sale_price', title: '累计消费', sort: true,},
            {title: '操作', toolbar: '#currentTableBar'}
        ]],
        limits: [10, 15, 20, 25, 30, 50, 100],
        limit: 10,
        page: true,
        skin: 'line,row',
        done: function (res, curr, count) {

        }
    });

    table.on('tool(currentTableFilter)', function (obj) {
        var data = obj.data;
        if (obj.event === 'delete') {

            layer.confirm('确认删除么？', function (index) {

                $.ajax({
                    url: '/admin/Member/MemberDel',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        id: data.id,

                    },
                    success: function(data) {

                        if (data.status) {
                            layer.msg('删除成功!', {icon: 1, time: 1000},function () {
                                $(obj)[0].tr.remove()
                                // location.reload()
                            });
                        }else{
                            layer.msg(data.msg, {
                                icon: 2,
                                time: 3000
                            });

                        }

                    },
                    error: function(re) {
                        layer.open({
                            title: '删除失败',
                            content: '<span style="color:red">'+re.responseJSON.errorCode+':</span>'+re.responseJSON.msg
                        })
                    }
                });
                // obj.del();
                // layer.close(index);
            });
        }

        if (obj.event === 'edit') {
            var index = layer.open({
                title: '用户详情',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/admin/Member/MemberDetail?id=' + data.id,
                end: function () {
                    /*删除session无用图片*/
                    $.ajax({
                        url: '/admin/Member/clearSession',
                        type: 'post',
                        dataType: 'json',
                    });
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }

        if(obj.event === 'thumb'){
            let aa={
                "title": data.name, //相册标题
                "id": 1, //相册id
                "start": 0, //初始显示的图片序号，默认0
                "data": [   //相册包含的图片，数组格式
                    {
                        "alt": data.name,
                        "pid": 2, //图片id
                        "src": data.headpic, //原图地址
                        "thumb": data.headpic //缩略图地址
                    }
                ]
            }
            layer.photos({
                photos: aa
                ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });

        }
    });



    // 监听搜索操作
    form.on('submit(data-search-btn)', function (data) {
        submitInfo();
    });
    form.on('select(selectInfo)', function (data) {
        submitInfo();
    });

    $("input[name='phone']").on('blur',function (re) {
        submitInfo();
    })


    function submitInfo() {
        var param = {};
        var t = $('#formInfo [name]');
        console.log(param)
        $.each(t, function () {
            param[this.name] = this.value;
        });
        //var result = JSON.stringify(d);

        //执行搜索重载
        table.reload('currentTableId', {
            page: {
                curr: 1
            }
            , where: {
                //searchParams: result
                param
            }
        }, 'data');
        return false;
    };
});
